package com.zyy.game.tianlong.network;

import com.zyy.game.tianlong.entity.GamePlayer;
import com.zyy.game.tianlong.entity.Player;
import io.netty.channel.ChannelFutureListener;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 游戏管理
 *
 * @author relufi
 * @date 2019/8/12 0024
 */
@Component
public class GameManage {
    private final Map<Integer, GameContext> context = new ConcurrentHashMap<>();
    public static final String ID_KEY = "playerId";

    public void put(Player player, GameContext gameContext) {
        gameContext.getContext().channel().attr(AttributeKey.<GamePlayer>valueOf(ID_KEY)).set(GamePlayer.convertPlayer(player));
        context.put(player.getPlayerId(), gameContext);
    }

    public Map<Integer, GameContext> getContext() {
        return context;
    }

    public GameContext get(int playerId) {
        return context.get(playerId);
    }

    public void remove(int playerId) {
        remove(playerId, context.get(playerId));
    }

    public static GamePlayer getGamePlayer(GameContext gameContext) {
        return gameContext.getContext().channel().attr(AttributeKey.<GamePlayer>valueOf(ID_KEY)).get();
    }

    public void remove(int playerId, GameContext gameContext) {
        context.remove(playerId, gameContext);
        gameContext.getContext().channel().attr(AttributeKey.valueOf(ID_KEY)).set(null);
    }

    public void remove(GameContext gameContext) {
        Attribute<GamePlayer> attr = gameContext.getContext().channel().attr(AttributeKey.<GamePlayer>valueOf(ID_KEY));
        context.remove(attr.get().getPlayerId(), gameContext);
        attr.set(null);
    }

    public void write(int playerId, SocketMsg socketMsg) {
        context.get(playerId).write(socketMsg);
    }

    public void writeFuture(int playerId, SocketMsg socketMsg) {
        context.get(playerId).write(socketMsg);
    }


    public void write(int playerId, int opCode, int subCode, Object data) {
        context.get(playerId).write(opCode, subCode, data);
    }


    public void writeAndFlush(int playerId, SocketMsg socketMsg) {
        context.get(playerId).writeAndFlush(socketMsg);
    }

    public void writeAndFlushFuture(int playerId, SocketMsg socketMsg) {
        context.get(playerId).writeAndFlush(socketMsg);
    }


    public void writeAndFlush(int playerId, int opCode, int subCode, Object data) {
        context.get(playerId).writeAndFlush(opCode, subCode, data);
    }

    public void flush(int playerId) {
        context.get(playerId).flush();
    }

    public void close(int playerId) {
        GameContext gameContext = context.get(playerId);
        gameContext.close();
        context.remove(playerId, gameContext);
    }

    public void writeFlushAndClose(int playerId, SocketMsg socketMsg) {
        GameContext gameContext = context.get(playerId);
        gameContext.writeAndFlushFuture(socketMsg).addListener(ChannelFutureListener.CLOSE);
        remove(playerId, gameContext);
    }
}
